package com.butang.utils;

/**
 * ClassName: DataConversion
 * Package: com.butang.pojo
 * Description:
 *
 * @Author BuTang
 * @Create 2023/7/6 19:11
 * @Version 1.0
 */
public class DataConversion {
    static int HexBinary_Max = 65535;
    static long DoubleHexBinary_Max = 4294967295L;


    // 十进制转换为十六位位二进制并自动补齐
    public static String decimalToBinary(int n) {

        if (n < 0 || n > HexBinary_Max) {
            throw new IllegalArgumentException("The input number must be between 0 and 65535.");
        }
        String binary = Long.toBinaryString(n);
        while (binary.length() < 16) {
            binary = "0" + binary;
        }
        return binary;
    }

    // 十进制转换为三十二位二进制并自动补齐
    public static String decimalToBinary1(long n) {

        if (n < 0 || n > DoubleHexBinary_Max) {
            throw new IllegalArgumentException("The input number must be between 0 and 65535.");
        }
        String binary = Long.toBinaryString(n);
        while (binary.length() < 32) {
            binary = "0" + binary;
        }
        return binary;
    }


    //无符号32位无符号转换为有符号
    public static long doubleUnsignedToSigned(long unsigned) {
        if (unsigned <= 2147483647) {
            return unsigned;
        } else {
            return -(DoubleHexBinary_Max + 1 - unsigned);
        }
    }

    //无符号16位转换为有符号

    public static long unsignedToSigned(long unsigned) {
        if (unsigned <= 65535 / 2) {
            return unsigned;
        } else {
            return -(HexBinary_Max + 1 - unsigned);
        }
    }
}
